﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.SoQuy
{
    public partial class frmChiTiet : Form
    {
        #region khởi tạo biến
        bool checkClose = false;
        Entities.SoQuy[] soquy;
        Entities.SoQuy save;
        Entities.SelectSoQuy temp;
        DateTime date;
        int i = 0;
        #endregion

        #region hàm khởi tạo
        public frmChiTiet(Entities.SoQuy[] soquy, Entities.SelectSoQuy temp, string dudauky, string ducuoiky)
        {
            InitializeComponent();
            date = Entitystatic.date;
            this.soquy = soquy;
            this.temp = temp;
            lbGiaTriMa.Text = soquy[0].MaTK;
            lbGiaTriTen.Text = soquy[0].TenTK;
            txtDuCuoiKy.Text = ducuoiky;
            txtDuDauKy.Text = dudauky;
            soquy[soquy.Length - 1] = new Entities.SoQuy(Format(Thu()), Format(Chi()), "Tổng Cộng");

            dgvHienThi.DataSource = soquy;
            fix();
            Ton();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.NgayLap].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.MaPhieu].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.DienGiai].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.Ton].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.PhatSinhNo].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.PhatSinhCo].Visible = true;
            dgvHienThi.Columns[Common.Constants.SoQuy.NgayLap].HeaderText = "Ngày Lập";
            dgvHienThi.Columns[Common.Constants.SoQuy.MaPhieu].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.SoQuy.DienGiai].HeaderText = "Ghi Chú";
            dgvHienThi.Columns[Common.Constants.SoQuy.Ton].HeaderText = "Tồn";
            dgvHienThi.Columns[Common.Constants.SoQuy.PhatSinhNo].HeaderText = "Phát Sinh Nợ";
            dgvHienThi.Columns[Common.Constants.SoQuy.PhatSinhCo].HeaderText = "Phát Sinh Có";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Thu
        public double Thu()
        {
            double tongthu = 0;
            for (int i = 0; i < soquy.Length - 1; i++)
            {
                tongthu += Convert.ToDouble(soquy[i].PhatSinhNo);
            }
            return tongthu;
        }
        #endregion

        #region Chi
        public double Chi()
        {
            double tongchi = 0;
            for (int i = 0; i < soquy.Length - 1; i++)
            {
                tongchi += Convert.ToDouble(soquy[i].PhatSinhCo);
            }
            return tongchi;
        }
        #endregion

        #region Tồn
        public void Ton()
        {
            try
            {
                Entities.SoQuy[] tem = (Entities.SoQuy[])dgvHienThi.DataSource;
                double ton = 0;
                for (int i = 0; i < tem.Length - 1; i++)
                {
                    ton = Convert.ToDouble(ton) + Convert.ToDouble(soquy[i].PhatSinhNo) - Convert.ToDouble(soquy[i].PhatSinhCo);
                    tem[i].Ton = Format(ton);
                }
                dgvHienThi.DataSource = tem;
                fix();
            }
            catch
            {
            }
        }
        #endregion

        #region Format
        public string Format(double a)
        {
            if (a >= 0 && a < 10)
                return a.ToString();
            if (a < 0)
                return String.Format("{0,-0:0,0}", a);
            else
                return String.Format("{0:0,0}", a);
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region xử lý grid view
        void XuLy()
        {
            try
            {
                if (save == null)
                {
                    return;
                }
                string objname = save.TruongNgam;
                string maphieu = save.MaPhieu;
                switch (objname)
                {
                    case Common.Constants.SoQuy.PhieuThu:
                        {
                            int so = 0;
                            foreach (Entities.PhieuThu item in temp.PhieuThu)
                            {
                                if (item.MaPhieuThu == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }

                            PhieuThu.frmXuLy b = new PhieuThu.frmXuLy(temp.PhieuThu[so], Common.Constants.Update);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.PhieuChi:
                        {

                            int so = 0;
                            foreach (Entities.PhieuChi item in temp.PhieuChi)
                            {
                                if (item.MaPhieuChi == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }

                            PhieuChi.frmXuLy b = new PhieuChi.frmXuLy(temp.PhieuChi[so], Common.Constants.Update);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.BanBuon:
                        {

                            int so = 0;
                            foreach (Entities.HDBanBuon item in temp.HDBanBuon)
                            {
                                if (item.MaHDBanBuon == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }

                            BanBuon.frmXuLy b = new BanBuon.frmXuLy(temp.HDBanBuon[so]);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.BanLe:
                        {
                            int so = 0;
                            foreach (Entities.HDBanLe item in temp.HDBanLe)
                            {
                                if (item.MaHDBanLe == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }

                            BanLe.frmXuLy b = new BanLe.frmXuLy(temp.HDBanLe[so]);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.HoaDonNhap:
                        {
                            int so = 0;
                            foreach (Entities.HoaDonNhap item in temp.HoaDonNhap)
                            {
                                if (item.MaHoaDonNhap == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }

                            //NhapKho.frmXuLy b = new NhapKho.frmXuLy(Common.Constants.GiaTriCacForm.SuaHoaDonNhap, temp.HoaDonNhap[so]);
                            //b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.KHTLBL:
                        {
                            int so = 0;
                            foreach (Entities.KhachHangTraLaiBanLe item in temp.KhachHangTraLaiBanLe)
                            {
                                if (item.MaKhachHangTraLaiBanLe == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }
                            //KhachHangTraLaiBanLe.frmXuLy b = new KhachHangTraLaiBanLe.frmXuLy(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanLe, temp.KhachHangTraLaiBanLe[so]);
                            //b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.KHTLBB:
                        {
                            int so = 0;
                            foreach (Entities.KhachHangTraLaiBanBuon item in temp.KhachHangTraLaiBanBuon)
                            {
                                if (item.MaKhachHangTraLaiBanBuon == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }
                            //KhachHangTraLaiBanBuon.frmXuLy b = new KhachHangTraLaiBanBuon.frmXuLy(Common.Constants.GiaTriCacForm.SuaKhachHangTraLaiBanLe, temp.KhachHangTraLaiBanBuon[so]);
                            //b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.TLNCC:
                        {
                            int so = 0;
                            foreach (Entities.TraLaiNCC item in temp.TraLaiNCC)
                            {
                                if (item.MaHDTraLaiNCC == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }
                            TraLaiNhaCungCap.frmXuLy b = new TraLaiNhaCungCap.frmXuLy(temp.TraLaiNCC[so]);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.PTTKH:
                        {
                            int so = 0;
                            foreach (Entities.PhieuTTCuaKH item in temp.PhieuTTCuaKH)
                            {
                                if (item.MaPhieuTTCuaKH == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }
                            PhieuTTCuaKH.frmXuLy b = new PhieuTTCuaKH.frmXuLy(temp.PhieuTTCuaKH[so], Common.Constants.Update);
                            b.ShowDialog();
                            break;
                        }
                    case Common.Constants.SoQuy.PTTNCC:
                        {
                            int so = 0;
                            foreach (Entities.PhieuTTNCC item in temp.PhieuTTNCC)
                            {
                                if (item.MaPhieuTTNCC == maphieu)
                                {
                                    break;
                                }
                                so++;
                            }
                            PhieuTTNCC.frmXuLy b = new PhieuTTNCC.frmXuLy(temp.PhieuTTNCC[so], Common.Constants.Update);
                            b.ShowDialog();
                            break;
                        }
                    default:
                        break;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Event
        private void frmChiTiet_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmChiTiet_FormClosing);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvhienthi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvhienthi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvhienthi_KeyDown);
        }

        void dgvhienthi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                XuLy();
            }
        }

        void dgvhienthi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            XuLy();
        }

        void dgvhienthi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void frmChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion